package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.provider.MainPagerAdapter;
import com.ohos.carbon.provider.ViewCreateHelper;
import com.ohos.carbon.LogUtils;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class TabsSlice extends AbilitySlice implements TabList.TabSelectedListener,
        PageSlider.PageChangedListener {

    private static final String TAG = "TabsSlice";
    private Image back_button, tabs_image_one, tabs_image_two, tabs_image_three,
            tabs_image_four, tabs_image_five, tabs_image_six, tabs_image_seven;
    private Text text_title;
    private TabList tabList;
    private PageSlider pageSlider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_tabs);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Tabs")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Tabs")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Tabs", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Tabs", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Tabs", 1);
            pf.flushSync();
        }
    }

    private void initData() {
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager);
        tabList = (TabList) findComponentById(ResourceTable.Id_tablist);
        String[] tabsTitle = {"PAGE 0", "PAGE 1", "PAGE 2", "PAGE 3", "PAGE 4", "PAGE 5", "PAGE 6"};
        for (String title : tabsTitle) {
            TabList.Tab tab = tabList.new Tab(this);
            tab.setText(title);
            tabList.addTab(tab);
        }
        tabList.selectTabAt(0);  //选中0位置
        tabList.setTabLength(200);
        tabList.setTabMargin(20);

        pageSlider.setProvider(new MainPagerAdapter(initPageSliderViewData()));
        pageSlider.setCurrentPage(0);
        pageSlider.setReboundEffect(true);
        pageSlider.setCentralScrollMode(true);
        tabList.setFixedMode(false);
    }

    private ArrayList<Component> initPageSliderViewData() {
        int pageSize = tabList.getTabCount();
        ArrayList<Component> pages = new ArrayList<>();
        ViewCreateHelper viewCreateHelper = new ViewCreateHelper(this);
        for (int i = 0; i < pageSize; i++) {
            pages.add(viewCreateHelper.createView(tabList.getTabAt(i).getText()));
        }
        return pages;
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Tabs");
//        radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_tabs_container);
//        tabs_radio_one = (RadioButton) findComponentById(ResourceTable.Id_tabs_radio_one);
//        tabs_radio_two = (RadioButton) findComponentById(ResourceTable.Id_tabs_radio_two);
//        tabs_radio_three = (RadioButton) findComponentById(ResourceTable.Id_tabs_radio_three);
//        tabs_radio_four = (RadioButton) findComponentById(ResourceTable.Id_tabs_radio_four);
//        tabs_radio_five = (RadioButton) findComponentById(ResourceTable.Id_tabs_radio_five);
//        tabs_radio_six = (RadioButton) findComponentById(ResourceTable.Id_tabs_radio_six);
//        tabs_radio_seven = (RadioButton) findComponentById(ResourceTable.Id_tabs_radio_seven);
//        Element element = null;
//        tabs_radio_one.setButtonElement(element);
//        tabs_radio_two.setButtonElement(element);
//        tabs_radio_three.setButtonElement(element);
//        tabs_radio_four.setButtonElement(element);
//        tabs_radio_five.setButtonElement(element);
//        tabs_radio_six.setButtonElement(element);
//        tabs_radio_seven.setButtonElement(element);
//        tabs_radio_one.setBackground(ElementScatter.getInstance(this).parse(ResourceTable.Media_radio_false));
        tabs_image_one = (Image) findComponentById(ResourceTable.Id_tabs_image1);
        tabs_image_one.setPixelMap(ResourceTable.Media_radio_true);
        tabs_image_two = (Image) findComponentById(ResourceTable.Id_tabs_image2);
        tabs_image_three = (Image) findComponentById(ResourceTable.Id_tabs_image3);
        tabs_image_four = (Image) findComponentById(ResourceTable.Id_tabs_image4);
        tabs_image_five = (Image) findComponentById(ResourceTable.Id_tabs_image5);
        tabs_image_six = (Image) findComponentById(ResourceTable.Id_tabs_image6);
        tabs_image_seven = (Image) findComponentById(ResourceTable.Id_tabs_image7);

        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
        tabList.addTabSelectedListener(this);
        pageSlider.addPageChangedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onPageSliding(int i, float v, int i1) {

    }

    @Override
    public void onPageSlideStateChanged(int i) {

    }

    @Override
    public void onPageChosen(int i) {
        LogUtils.d(TAG, i + "onPageChosen111");
        if (tabList.getSelectedTab().getPosition() != i) {
            tabList.selectTabAt(i);
        }
        switch (i) {
            case 0:
                tabs_image_one.setPixelMap(ResourceTable.Media_radio_true);
                tabs_image_two.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_three.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_four.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_five.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_six.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_seven.setPixelMap(ResourceTable.Media_radio_false);
                break;
            case 1:
                tabs_image_one.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_two.setPixelMap(ResourceTable.Media_radio_true);
                tabs_image_three.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_four.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_five.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_six.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_seven.setPixelMap(ResourceTable.Media_radio_false);
                break;
            case 2:
                tabs_image_one.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_two.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_three.setPixelMap(ResourceTable.Media_radio_true);
                tabs_image_four.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_five.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_six.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_seven.setPixelMap(ResourceTable.Media_radio_false);
                break;
            case 3:
                tabs_image_one.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_two.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_three.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_four.setPixelMap(ResourceTable.Media_radio_true);
                tabs_image_five.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_six.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_seven.setPixelMap(ResourceTable.Media_radio_false);
                break;
            case 4:
                tabs_image_one.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_two.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_three.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_four.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_five.setPixelMap(ResourceTable.Media_radio_true);
                tabs_image_six.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_seven.setPixelMap(ResourceTable.Media_radio_false);
                break;
            case 5:
                tabs_image_one.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_two.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_three.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_four.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_five.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_six.setPixelMap(ResourceTable.Media_radio_true);
                tabs_image_seven.setPixelMap(ResourceTable.Media_radio_false);
                break;
            case 6:
                tabs_image_one.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_two.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_three.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_four.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_five.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_six.setPixelMap(ResourceTable.Media_radio_false);
                tabs_image_seven.setPixelMap(ResourceTable.Media_radio_true);
                break;
        }
    }

    @Override
    public void onSelected(TabList.Tab tab) {
        if (pageSlider.getCurrentPage() != tab.getPosition()) {
            pageSlider.setCurrentPage(tab.getPosition());
        }
    }

    @Override
    public void onUnselected(TabList.Tab tab) {

    }

    @Override
    public void onReselected(TabList.Tab tab) {

    }
}
